package com.jjkj.environmental.environmental.ui

import android.os.Bundle
import android.view.View
import com.jjkj.environmental.environmental.BuildConfig
import com.jjkj.environmental.environmental.R
import com.jjkj.environmental.environmental.bean.model.SettingType
import com.jjkj.environmental.environmental.bean.model.UserInfo
import com.jjkj.environmental.environmental.presenter.LoginPresenter
import com.jjkj.environmental.environmental.ui.base.BaseActivity
import com.jjkj.environmental.environmental.ui.dialog.EdtDialog
import com.jjkj.environmental.environmental.utils.LitePalHelper
import com.jjkj.environmental.environmental.view.LoginView
import com.nightonke.boommenu.BoomButtons.HamButton
import kotlinx.android.synthetic.main.activity_login.*
import java.util.regex.Pattern


class LoginActivity : BaseActivity<LoginView, LoginPresenter>(), LoginView, View.OnClickListener {

    private val setting = listOf(
            SettingType("设置服务器地址", "", R.mipmap.computer),
            SettingType("检测站站号", "", R.mipmap.station)
    )
    private val ipRegex = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"

    override fun initInject() {
        getComponent().inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView()
    }

    private fun initView() {
        tv_version.append(BuildConfig.VERSION_NAME)
        val userSetting = LitePalHelper.getUserSetting()
        setting.forEach {
            when (it.SettingLogo) {
                R.mipmap.computer -> it.settingValue = userSetting.ipAddress
                R.mipmap.station -> it.settingValue = userSetting.station
            }
        }
        for (i in 0 until bmb_menu.piecePlaceEnum.pieceNumber()) {
            val builder = HamButton.Builder()
                    .normalImageRes(setting[i].SettingLogo)
                    .normalText(setting[i].settingValue)
                    .subNormalText(setting[i].settingName)
                    .listener {
                        EdtDialog(this, object : EdtDialog.OnChoose {
                            override fun choose(content: String, type: Int) {
                                when (type) {
                                    0 -> {
                                        if (!isIpAddress(content)) {
                                            showToast("非法的IP地址")
                                            return
                                        }
                                        userSetting.ipAddress = content
                                        getPresenter().updateIpAddress(content)
                                    }
                                    1 -> userSetting.station = content
                                }
                                if (userSetting.save()) {
                                    val builder = bmb_menu.builders[i]
                                    if (builder is HamButton.Builder) {
                                        builder.normalText(content)
                                    }
                                    showToast("修改成功")
                                }
                                setting[type].settingValue = content
                            }
                        }, i).show()
                    }
            bmb_menu.addBuilder(builder)
        }
    }

    private fun isIpAddress(ipAddress: String): Boolean {
        val pattern = Pattern.compile(ipRegex)
        return pattern.matcher(ipAddress).matches()
    }

    private fun login() {
        if (edt_username.text.toString() == "" || edt_password.text.toString() == "") {
            showToast("用户名和密码不能为空")
            return
        }
        val userInfo = UserInfo(edt_username.text.toString(), edt_password.text.toString())
        getPresenter().login(userInfo)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_login -> login()
        }
    }

    override fun onDataLoad(bean: String) {
        MainActivity.jumpTo(this)
    }
}
